package com.ruoyi.gp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gp.domain.bo.AuditBo;
import com.ruoyi.gp.domain.vo.AuditVo;
import com.ruoyi.gp.domain.Audit;
import com.ruoyi.gp.mapper.AuditMapper;
import com.ruoyi.gp.service.IAuditService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 审核Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-14
 */
@RequiredArgsConstructor
@Service
public class AuditServiceImpl implements IAuditService {

    private final AuditMapper baseMapper;

    /**
     * 查询审核
     */
    @Override
    public AuditVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询审核列表
     */
    @Override
    public TableDataInfo<AuditVo> queryPageList(AuditBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Audit> lqw = buildQueryWrapper(bo);
        Page<AuditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询审核列表
     */
    @Override
    public List<AuditVo> queryList(AuditBo bo) {
        LambdaQueryWrapper<Audit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Audit> buildQueryWrapper(AuditBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Audit> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAuditName()), Audit::getAuditName, bo.getAuditName());
        lqw.eq(StringUtils.isNotBlank(bo.getApplicants()), Audit::getApplicants, bo.getApplicants());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditBy()), Audit::getAuditBy, bo.getAuditBy());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), Audit::getAuditStatus, bo.getAuditStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), Audit::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), Audit::getReason, bo.getReason());
        lqw.eq(bo.getDoctorId() != null, Audit::getDoctorId, bo.getDoctorId());
        return lqw;
    }

    /**
     * 新增审核
     */
    @Override
    public Boolean insertByBo(AuditBo bo) {
        Audit add = BeanUtil.toBean(bo, Audit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改审核
     */
    @Override
    public Boolean updateByBo(AuditBo bo) {
        Audit update = BeanUtil.toBean(bo, Audit.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Audit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除审核
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<AuditVo> queryByDoctorId(long doctorId,String status, PageQuery pageQuery) {
        AuditBo bo=new AuditBo();
        if(ObjectUtil.isNotEmpty(status)){
            bo.setAuditStatus(status);
        }
        bo.setDoctorId(doctorId);
        LambdaQueryWrapper<Audit> lqw = buildQueryWrapper(bo);
        Page<AuditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
